package com.xmkj.bearuser.membercenter;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.CollectionBean;
import com.common.retrofit.bean.RewardDetailsBean;
import com.common.retrofit.bearusermethod.CollectionMethods;
import com.common.retrofit.bearusermethod.GetMyCollectionListMethods;
import com.common.retrofit.entity.bean.ShopCarBean;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.adapter.CommonAdapter;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.common.widget.recyclerview.refresh.recycleview.swipemenu.SwipeMenuRecycler;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.membercenter.adapter.CollectionAdapter;
import com.xmkj.bearuser.membercenter.adapter.ShareRewardAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/7/17
 * 描述：
 */

public class MyCollectionActivity extends BaseMvpActivity {
    @BindView(R.id.recyclerview)
    SwipeMenuRecycler recyclerview;
    List<CollectionBean> bean = new ArrayList<>();
    private CollectionAdapter adapter;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collection;
    }

    @Override
    protected void onViewCreated() {
    }

    @Override
    protected void doLogicFunc() {
        setRecyclerView();
        gotoHttpRep();
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("我的收藏");
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);

    }
    private void setRecyclerView() {
//        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
       /* GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);*/
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);


//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setLoadMoreEnabled(true);
        adapter = new CollectionAdapter(context, bean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new CommonAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(CommonAdapter adapter, View view, int position) {
                CollectionBean bean = (CollectionBean) adapter.mData.get(position);
                if (view.getId() == R.id.btn_delete) {
                    //删除购物车6c29a76295f1f10e88e67dd66770f037
                    deleteShopCar(position, bean.gid);
                }
            }
        });
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }

    private void deleteShopCar(final int position, int gid) {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                adapter.remove(position);
                bean.remove(position);

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        CollectionMethods.getInstance().goodCollection(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), gid+"",2+"");
        rxManager.add(subscriber);
    }

    private void gotoHttpRep() {
        CommonSubscriber<ArrayList<CollectionBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<CollectionBean> beans = (ArrayList<CollectionBean>) o;

                List<CollectionBean> beanList =  beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetMyCollectionListMethods.getInstance().getMyCollection(subscriber, SPUtils.getShareInt(Constans.UID)+"", SPUtils.getShareString(Constans.HASHID), mPageIndex);
        rxManager.add(subscriber);
    }
}
